package com.wlz.file.threadfile;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.concurrent.CountDownLatch;

public class FileThread extends Thread {
 
        private String fileUrl;
        private String copyUrl;
        private long position;
        private long count;
        private CountDownLatch cdl;
 
        public FileThread(String fileUrl, String copyUrl, long position, long count, CountDownLatch cdl) {
            this.fileUrl = fileUrl;
            this.copyUrl = copyUrl;
            this.position = position;
            this.count = count;
            this.cdl = cdl;
        }
 
        @Override
        public void run() {
            RandomAccessFile fileRaf = null;
            RandomAccessFile copyRaf = null;
            FileChannel fileChannel = null;
            FileChannel copyChannel = null;
            try {
                fileRaf = new RandomAccessFile(new File(fileUrl), "rw");
                copyRaf = new RandomAccessFile(new File(copyUrl), "rw");
 
                fileChannel = fileRaf.getChannel();
                copyChannel = copyRaf.getChannel();
 
                copyChannel.position(position);
                fileChannel.position(position);
 
                //使用两个方法进行测试
                fileChannel.transferTo(position, count, copyChannel);
//                copyChannel.transferFrom(fileChannel, position, count);
                
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                cdl.countDown();
                BlogTest.closeStream(copyChannel, fileChannel, copyRaf, fileRaf);
            }
        }
    }